package com.zm.shop.controller;

import com.zm.shop.common.constants.CarCons;
import com.zm.shop.common.entity.GoodsEntity;
import com.zm.shop.common.utils.R;
import com.zm.shop.entity.CarEntity;
import com.zm.shop.feign.GoodsService;
import com.zm.shop.interceptor.CarInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Controller
@Slf4j
@RequestMapping("/car")
public class CarController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CarInterceptor carInterceptor;

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/addGoodsToCar")  //添加商品到购物车
    @ResponseBody
    public R addGoodsToCar(@RequestBody CarEntity carEntity){
        //获取当前用户ID
        String userCarId = carInterceptor.getThreadLocal();
        // Redis hash绑定 （field key value 存储模式）
        BoundHashOperations<String, Object, Object> carHashMap = stringRedisTemplate.boundHashOps(CarCons.CAR_PRE + userCarId);
        //获取当前商品ID和数量
        String key = carEntity.getGid().toString();
        String val = carEntity.getCount().toString();
        //添加到redis
        carHashMap.increment(key,Long.parseLong(val));
        return R.ok();
    }
    @GetMapping("/getCarList")  //获取购物车清单
    public String getCarList(ModelMap modelMap) {
        List<CarEntity> carList = new ArrayList<>();
        // 1.获取用户购物车的id
        String userCarId = carInterceptor.getThreadLocal();
        // 2.Redis hash绑定
        BoundHashOperations<String, Object, Object> hashOperations = stringRedisTemplate.boundHashOps(CarCons.CAR_PRE + userCarId);
        // 3.获取所有商品id
        Set<Object> keys = hashOperations.keys();
        for (Object key : keys) { //获取存在reids里CarCons.CAR_PRE下 用户的购物车的商品ID和数量
            int gid = Integer.parseInt(key.toString());
            int count = Integer.parseInt(hashOperations.get(key).toString());
            GoodsEntity goodsEntity = goodsService.getGoodsEntityById(gid);
            BigDecimal subTotal = goodsEntity.getGprice().multiply(BigDecimal.valueOf(count));//计算商品总价

            CarEntity car = new CarEntity();
            car.setGid(gid);
            car.setCount(count);
            car.setSubTotal(subTotal);
            carList.add(car);
        }
        modelMap.put("carList",carList);
        return "carList";
    }

    @GetMapping("/getCarCount") //获取购物车订单数量
    @ResponseBody
    public Integer getCarCount(HttpServletRequest request) {
        // 1.获取用户购物车的id
        String userCarId = carInterceptor.getThreadLocal();
        // 2.Redis hash绑定
        BoundHashOperations<String, Object, Object> carHashMap = stringRedisTemplate.boundHashOps(CarCons.CAR_PRE + userCarId);
        return carHashMap.keys().size();
    }

    @PostMapping("/updateCarCount")     //  购物车里加减商品数量
    @ResponseBody
    public R updateCarCount(@RequestBody CarEntity car) {
        log.debug("car:{}", car);
        // 1.获取用户购物车的id
        String userCarId = carInterceptor.getThreadLocal();
        // 2.Redis hash绑定
        BoundHashOperations<String, Object, Object> carMap = stringRedisTemplate.boundHashOps(CarCons.CAR_PRE + userCarId);
        //获取商品ID
        String key = car.getGid().toString();
        // 判断自增还是自减
        switch (car.getStatus()) {
            case "add":
                carMap.increment(key, 1); // 给KEY的value值自增
                break;
            case "sub":
                carMap.increment(key, -1);
                break;
            default:
                break;
        }
        return R.ok();
    }

}
